<?php
/**
 * 默认过滤器 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: func_filter.php 520 2010-06-22 10:12:33Z lisijie86 $
*/

!defined('IN_JBLOG') && exit('Access Denied!');

//ubb解码
function ubb_decode($string) {
	$blogurl = config('blogurl');
	$string = str_replace(array('[b]','[/b]'),array('<b>','</b>'),$string);
	$string = preg_replace('/\[upload=([0-9]+)]/ies',"url('mod=attach&id=\\1')",$string);
	if ( false !== strpos($string, '[/img]') ) {
		$string = preg_replace('/\[img]\s*([^\[\r\n\<]+?)\s*\[\/img]/ise',"'<img src=\"'.url_com('\\1').'\" onload=\"resize_img(this)\" />'",$string);
		$string = preg_replace('/\[img=(\d{1,4}),(\d{1,4})]\s*([^\[\r\n\<]+?)\s*\[\/img]/ise',"'<img src=\"'.url_com('\\3').'\" width=\"\\1\" height=\"\\2\" onload=\"resize_img(this)\" />'",$string);
	}
	$string = preg_replace('/\[file]\s*([^\[]+?)\s*\[\/file]/is','<a href="\\1">点击下载附件</a>',$string);
	return $string;
}

function url_com($url) {
	if ( substr($url,0,4) != 'http' && substr($url,0,1) != '/' ) {
		$url = config('blogurl').'/'.$url;
	}
	return $url;
}
add_filter('post_content', 'ubb_decode');
add_filter('post_excerpt', 'ubb_decode');


function comment_code($string) {
	$string = preg_replace('/\[re=(\d+),([^\]]+?)]/is', '<a rel="nofollow" href="#comment-\\1" class="comment_quote">@\\2</a> ', $string);
	return $string;
}
add_filter('comment_content', 'comment_code');


//评论回复邮件通知
function comment_reply_mail($comment) {
	global $_CACHE, $db, $_USER;
	get_cache('mail');
	if ( empty($_CACHE['mail']['reply_mail']) || empty($_CACHE['mail']['reply_mail_subject']) || empty($_CACHE['mail']['reply_mail_body']) ) {
		return ;
	}
	//仅已登录用户回复时发送邮件
	if ( $_CACHE['mail']['reply_mail_type'] == 2 && !$_USER['id'] ) {
		return ;
	}
	require_once JBLOG_INC.'func_post.php';
	require_once JBLOG_INC.'class_mailer.php';
	$post = get_post($comment['postid']);
	//仅文章作者或管理员回复时发送邮件
	if ( $_CACHE['mail']['reply_mail_type'] == 3 && ( !$_USER['id'] || ($_USER['group'] != 'admin' && $_USER['id'] != $post['userid']) ) ) {
		return ;
	}
	//检查评论内容的回复代码
	preg_match_all('/\[re=(\d+),([^\]]+?)]/i', $comment['content'], $matches);
	$reids = $matches[1];
	if ( $reids ) {
		$comment['content'] = preg_replace('/\[re=(\d+),([^\]]+?)]/is','@\\2 ', $comment['content']);
		$subject = str_replace(array('{blogname}','{postname}','{re_author}'), array(config('blogname'),$post['title'],$comment['author']),$_CACHE['mail']['reply_mail_subject']);
		$body_search = array(
			'{blogname}','{blogurl}','{postname}','{commenturl}','{re_author}','{re_date}','{re_content}'
		);
		$body_replace = array(
			config('blogname'), config('blogurl'), $post['title'], 
			config('blogurl').'/'.url('mod=post&id='.$post['id'].'&alias='.$post['alias']), $comment['author'], 
			get_date($comment['dateline']), $comment['content']
		);
		$body = str_replace($body_search, $body_replace, $_CACHE['mail']['reply_mail_body']);
		$reids = array_unique($reids);
		filter_ids($reids);
		$query = $db->query("SELECT postid, userid, author, email, content, dateline FROM ".tname('comment')." 
		WHERE id IN (".simplode($reids).") AND `status` = '1' AND `postid` = '{$post['id']}'");
		while ( $row = $db->fetch_array($query) ) {
			//回复自己的评论忽略
			if ( ! is_email($row['email']) || ( $comment['userid'] && $comment['userid'] == $row['userid'] ) ) {
				continue;
			}
			$row['content'] = preg_replace('/\[re=(\d+),([^\]]+?)]/is','@\\2 ', $row['content']);
			$subject = str_replace('{or_author}', $row['author'], $subject);
			$body_search = array('{or_author}','{or_date}','{or_content}');
			$body_replace = array($row['author'], get_date($row['dateline']), $row['content']);
			$body = str_replace($body_search, $body_replace, $body);
			$result = sendmail($subject, $body, $row['email'], $row['author']);
		}
	}
}
add_action('post_comment', 'comment_reply_mail');
add_action('comment_reply_submit', 'comment_reply_mail');

?>